// pages/word/index.js
import tool from "../../utils/tool.js";
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    dataarr: [],
    leftarr: [],
    leinde: 1,
    type: '',
    caid: '',
    typeindex: '1',
    datatype: null,
    inputvalue:''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    wx.request({
      url: app.globalData.url + '/zkxj/business/school/proFileStash/list',
      data: {
        author: "",
        category: null,
        fileType: null,
        title: ""
      },
      method: 'POST',
      header: {
        'content-type': 'application/json',
        'token': wx.getStorageSync('token')
      },
      success: (res) => {
        console.log(res.data.code);
        if (res.data.code == 0) {

          this.setData({
            dataarr: res.data.data
          })


        }
      },
    })

    wx.request({
      url: app.globalData.url + '/zkxj/business/school/proFileStash/category',
      data: {

      },
      method: 'GET',
      header: {
        'content-type': 'application/json',
        'token': wx.getStorageSync('token')
      },
      success: (res) => {
        console.log(res.data.code);
        if (res.data.code == 0) {
          this.setData({
            leftarr: res.data.data
          })
        }
      },
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },
  onchangein: tool.throttle(function (e) {  
    this.setData({
      inputvalue:e[0].detail
    })
    wx.request({
      url: app.globalData.url+'/zkxj/business/school/proFileStash/list',
      data: {
        author: "",
        category: null,
        fileType: null,
        title:e[0].detail
      },
      method: 'POST',
      header: {
        'content-type': 'application/json',
        'token': wx.getStorageSync('token')
      },
      success: (res) => {
        if (res.data.code == 0) {
          this.setData({
            dataarr: res.data.data
          })
        }
      },
    })
  }),
  handetype(e) {
    const type = e.currentTarget.dataset['type'] || null
    const typess = e.currentTarget.dataset['typein'] || 1
    console.log(type, typess);
    this.setData({
      typeindex: typess,
      datatype: type
    })
    wx.request({
      url: app.globalData.url + '/zkxj/business/school/proFileStash/list',
      data: {
        author: "",
        category: this.data.caid == 1 ? null : this.data.caid,
        currPage: 1,
        fileType: this.data.datatype == 0 ? null : this.data.datatype,
        pageSize: 100,
        title: ""
      },
      method: 'POST',
      header: {
        'content-type': 'application/json',
        'token': wx.getStorageSync('token')
      },
      success: (res) => {
        if (res.data.code == 0) {
          this.setData({
            dataarr: res.data.data
          })
        }
      },
    })
  },
  handecheck(e) {
    const goodid = e.currentTarget.dataset['index'] || 0
    const caid = e.currentTarget.dataset['caid'] || 0
    this.setData({
      leinde: goodid,
      caid: caid
    })
    wx.request({
      url: app.globalData.url + '/zkxj/business/school/proFileStash/list',
      data: {
        author: "",
        category: caid == 1 ? null : caid,
        currPage: 1,
        fileType: this.data.datatype,
        pageSize: 100,
        title: ""
      },
      method: 'POST',
      header: {
        'content-type': 'application/json',
        'token': wx.getStorageSync('token')
      },
      success: (res) => {
        console.log(res.data.code);
        if (res.data.code == 0) {

          this.setData({
            dataarr: res.data.data
          })
        }
      },
    })




  },
  handletopde(e) {
    const typena=e.currentTarget.dataset['typena'] 
    console.log(typena);
    const urls = e.currentTarget.dataset['urls']
    if(typena=='pdf'){
      wx.downloadFile({
        url: urls,//pdf地址 例如：http://**.*****.***/ceshi/demo.pdf
       
        success(res) {
          console.log(res);
            if (res.statusCode === 200) {
              const tempFilePath = res.tempFilePath//返回的文件临时地址，用于后面打开本地预览所用
              console.log(tempFilePath);
                wx.openDocument({
                    filePath: tempFilePath,
                    showMenu: true,
                    fileType: "pdf",
                    success: function (res) {}
                })
            } else {
            }
        },
        fail(res) {
          console.log(res);
  
        }
    })
    }else if(typena=='jpg'){
    console.log(urls);

wx.navigateTo({
      url: `/pages/wordetail/index?urls=${encodeURIComponent(urls)}`,
    })
    }else if(typena=='ppt'){
      wx.downloadFile({
        url: urls,//pdf地址 例如：http://**.*****.***/ceshi/demo.pdf
       
        success(res) {
          console.log(res);
            if (res.statusCode === 200) {
              const tempFilePath = res.tempFilePath//返回的文件临时地址，用于后面打开本地预览所用
              console.log(tempFilePath);
                wx.openDocument({
                    filePath: tempFilePath,
                    showMenu: true,
                    fileType: "pptx",
                    success: function (res) {}
                })
            } else {
            }
        },
        fail(res) {
          console.log(res);
  
        }
    })
    }
   
    
    
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})